package com.study.demo.es;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class EsService {

    @Autowired
    private RestHighLevelClient client;

    /**
     *
     */
    public void queryPage() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        searchSourceBuilder.query(boolQueryBuilder);

        // sell
        String[] fields=new String[]{"goods_code", "goods_name", "sell_type", "energy_model",
                "car_series_code", "car_series_name", "car_model_code", "car_model_name",
                "car_brand_code", "car_brand_name", "material_type", "material_type_text",
                "energy_shape", "energy_shape_text", "goods_attribute", "goods_attribute_text",
                "description", "province_code", "province_name", "city_code", "city_name",
                "county_code", "county_name", "freight_type", "freight_type_text",
                "price", "goods_state", "goods_state_text", "remark", "urls", "cover_url"};


        // buy
        String[] fields2=new String[]{"goods_code", "goods_name",
                "demand", "change_type", "change_type_text", "energy_model",
                "category_type","category_type_text", "material_type", "material_type_text", "province_code",
                "province_name", "city_code", "city_name", "county_code", "county_name",
                "detail_address", "freight_type", "freight_type_text", "description", "price", "remark"};

        searchSourceBuilder.fetchSource(fields, null);


        //查询数据做冗余，足够后边剔除删除的数据
//        searchSourceBuilder.size(collectDto.getSize() + collectDto.getPageSize());
//        searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));
//        searchRequest.indices(esResource.getPersonalCollectIndex()).types(esResource.getPersonalCollectType());
        //按照创建时间倒序
//        searchSourceBuilder.sort(new FieldSortBuilder("create_time")
//                        .order(SortOrder.DESC))
//                .sort(new FieldSortBuilder("id")
//                        .order(SortOrder.ASC));

        searchRequest.source(searchSourceBuilder);

        log.debug("查询语句：{}", searchRequest.toString());
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        if (searchResponse.status() == RestStatus.OK) {
            SearchHits hits = searchResponse.getHits();
            long totalHits = hits.getTotalHits();

//            Page page = new Page<>(collectDto.getPageNo(), collectDto.getPageSize(), totalHits, idList);


        } else {
//            return Result.buildFailResult("查询收藏信息失败");
        }
    }
}
